home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / pyshared / launchpadbugs / html_blueprintlist.py < prev    next >
Encoding:
Python Source  |  2008-08-05  |  6.7 KB  |  177 lines

  1. import re
  2. import libxml2
  3. import urlparse
  4. from exceptions import parse_error
  5. from blueprintbase import BlueprintInfo, LPBluePrint
  6. from buglistbase import LPBugList, LPBugPage
  7. from lphelper import blueprint_sort, user, unicode_for_libxml2
  8. from lpconstants import BASEURL
  9. from utils import valid_lp_url
  10.  
  11. #deactivate error messages from the validation [libxml2.htmlParseDoc]
  12. def noerr(ctx, str):
  13.     pass
  14.  
  15. libxml2.registerErrorHandler(noerr, None)
  16.  
  17. class BPInfo(BlueprintInfo):
  18.     def __init__(self, priority, spec, title, url, design, delivery, assignee, project, mentorship):
  19.         url = valid_lp_url(url, BASEURL.BLUEPRINT)
  20.         BlueprintInfo.__init__(self, priority, spec, title, url, design, delivery, assignee, project, mentorship)
  21.         
  22.  
  23. class Project(object):
  24.     def __init__(self, url, project):
  25.         self.url = valid_lp_url(url, BASEURL.BLUEPRINT)
  26.         self.project = project
  27.     def __str__(self):
  28.         return str(self.project)
  29.         
  30.  
  31. class BlueprintPage(LPBugPage):
  32.     """
  33.     grab content of a single BluePrint-table    
  34.     """
  35.     @staticmethod
  36.     def find_parse_function(connection, url, all_tasks):
  37.         url = valid_lp_url(url, BASEURL.BLUEPRINTLIST)
  38.         lp_content = connection.get(url)
  39.         xmldoc = libxml2.htmlParseDoc(unicode_for_libxml2(lp_content.text), "UTF-8")
  40.         u = urlparse.urlsplit(url)
  41.         if "+milestone" in u[2]:
  42.             result = BlueprintPage.parse_html_milestone_specs(xmldoc, all_tasks, url)
  43.         else:
  44.             result = BlueprintPage.parse_html_blueprintpage(xmldoc, all_tasks, url)
  45.         return result
  46.         
  47.     @staticmethod
  48.     def parse_html_blueprintpage(xmldoc, all_tasks, url):
  49.         def _parse():
  50.             if not xmldoc.xpathEval('//table[@id="speclisting"]'):
  51.                 xmldoc.freeDoc()
  52.                 return
  53.                 
  54.             blueprinttable = xmldoc.xpathEval('//table[@id="speclisting"]//tbody//tr')
  55.             for row in blueprinttable:
  56.                 m = row.xpathEval('td[1]//span[not(@class="sortkey")]')
  57.                 assert m
  58.                 priority = m[0].prop("class")
  59.                 
  60.                 m = row.xpathEval('td[2]//a')
  61.                 assert m
  62.                 url = m[0].prop("href")
  63.                 title = m[0].prop("title")
  64.                 spec = m[0].content
  65.                 
  66.                 mentorship = bool(row.xpathEval('td[2]//img[@alt="mentoring"]'))
  67.                 #add INFORMATIONAL
  68.                 
  69.                 m = row.xpathEval('td[3]//span[not(@class="sortkey")]')
  70.                 assert m
  71.                 status = m[0].prop("class")
  72.                 
  73.                 m = row.xpathEval('td[4]//span[not(@class="sortkey")]')
  74.                 assert m
  75.                 delivery = m[0].prop("class")
  76.                 
  77.                 m = row.xpathEval('td[5]//a')
  78.                 if m:
  79.                     assignee = user.parse_html_user(m[0])
  80.                 else:
  81.                     assignee = user(None)
  82.                 
  83.                 m = row.xpathEval('td[6]//a')
  84.                 # on personal blueprint pages this column does not exist
  85.                 if m:
  86.                     project = Project(m[0].prop("href"), m[0].content)
  87.                 else:
  88.                     project = None
  89.                 
  90.                 yield BPInfo(priority, spec, title, url, status, delivery, assignee, project, mentorship)
  91.  
  92.         next = xmldoc.xpathEval('//div[@class="lesser"]//a[@rel="next"]//@href')
  93.         m = xmldoc.xpathEval('//td[@class="batch-navigation-index"]')
  94.         if m:
  95.             m = m.pop()
  96.             n = re.search(r'(\d+)\s+results?', m.content)
  97.             parse_error(n, "BugPage.parse_html_bugpage.length", url=url)
  98.             length = n.group(1)
  99.             n = m.xpathEval("strong")
  100.             batchsize = int(n[1].content) - int(n[0].content) + 1
  101.         else:
  102.             length = batchsize = 0
  103.         if next:
  104.             return _parse(), next[0].content, batchsize, int(length)
  105.         return _parse(), False, batchsize, int(length)
  106.         
  107.     
  108.     @staticmethod
  109.     def parse_html_milestone_specs(xmldoc, all_tasks, url):
  110.         def _parse():
  111.             if not xmldoc.xpathEval('//table[@id="milestone_specs"]'):
  112.                 xmldoc.freeDoc()
  113.                 return
  114.                 
  115.             blueprinttable = xmldoc.xpathEval('//table[@id="milestone_specs"]//tbody//tr')
  116.             for row in blueprinttable:
  117.                 m = row.xpathEval('td[1]//a')
  118.                 assert m
  119.                 url = m[0].prop("href")
  120.                 title = m[0].prop("title")
  121.                 spec = m[0].content
  122.                 
  123.                 m = row.xpathEval('td[2]//span[not(@class="sortkey")]')
  124.                 assert m
  125.                 priority = m[0].prop("class")
  126.                 
  127.                 # is the mentorship-icon used?
  128.                 mentorship = bool(row.xpathEval('td[2]//img[@alt="mentoring"]'))
  129.                 #add INFORMATIONAL
  130.                 
  131.                 m = row.xpathEval('td[3]/a')
  132.                 if m:
  133.                     assignee = user.parse_html_user(m[0])
  134.                 else:
  135.                     assignee = user(None)
  136.                 
  137.                 m = row.xpathEval('td[4]//span[not(@class="sortkey")]')
  138.                 assert m
  139.                 delivery = m[0].prop("class")
  140.                 
  141.                 yield BPInfo(priority, spec, title, url, None, delivery, assignee, None, mentorship)
  142.  
  143.         next = xmldoc.xpathEval('//a[@rel="next"]//@href')
  144.         m = xmldoc.xpathEval('//h2[@id="specification-count"]')
  145.         length = batchsize = int(m[0].content.split(" ")[0])
  146.         assert not next, "milestone_specs are supposed to be single-paged, does it changed? url='%s'" %url
  147.         return _parse(), False, batchsize, length
  148.         
  149.  
  150. class BlueprintList(LPBugList):
  151.     """
  152.     returns a SET of LPBugInfo objects
  153.     searches baseurl and its following pages
  154.     """
  155.     def __init__(self, baseurl, connection=None, all_tasks=False, progress_hook=None):
  156.         if hasattr(baseurl, "baseurl"):
  157.             baseurl.baseurl = valid_lp_url(baseurl.baseurl, BASEURL.BLUEPRINTLIST)
  158.         else:
  159.             baseurl = valid_lp_url(baseurl, BASEURL.BLUEPRINTLIST)
  160.         LPBugList.__init__(self, baseurl, connection, all_tasks,
  161.                     BlueprintPage, progress_hook)
  162.         
  163.     def __repr__(self):
  164.         return "<BlueprintList %s>" %self.baseurl.split("?")[0]
  165.         
  166.     def __str__(self):
  167.         return "BlueprintList([%s])" %",".join(repr(i) for i in self)
  168.     
  169.     def sort(self, optsort):
  170.         """ returns a LIST of bugs sorted by optsort """
  171.         return sorted(self, cmp=lambda x,y: blueprint_sort(x,y,optsort.strip("-")),
  172.                         reverse=optsort.startswith("-"))
  173.         
  174.     def add(self, item):
  175.         assert isinstance(item, (BlueprintInfo, LPBluePrint))
  176.         LPBugList.add(self, item)
  177.